package com.cy.jxc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.jxc.MyException.ServiceExecption;
import com.cy.jxc.dao.RegisterDao;
import com.cy.jxc.entity.User;
import com.cy.jxc.service.RegistService;
import com.cy.jxc.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.naming.Name;
import java.util.concurrent.TimeUnit;

@Service
public class RegistServiceImpl implements RegistService {
    private  static final String NAME="mailnum";

    @Autowired
    RegisterDao registerDao;

    @Autowired
    JavaMailSender javaMailSender;


    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public void sendMail(String from,String to,String subject,String content) {
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    @Override
    public void saveMailnum(String num) {
//        String name="mailnum";
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.opsForHash().put(NAME,"number",num);
      /*  redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForHash().put(name,"number",num);*/
        System.out.println("存入redis中");
        stringRedisTemplate.expire(NAME, 1000, TimeUnit.SECONDS);
    }

    @Override
    public String getMailNum() {
//        String name="mailnum";
        stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
        String number = (String) stringRedisTemplate.opsForHash().get(NAME, "number");
        System.out.println("获取redis缓存"+number);
        if (StringUtils.isEmpty(number)){
            throw  new ServiceExecption("请先进行邮箱验证");
        }
        return number;
    }

    @Override
    @Transactional
    public void register(RegisterVo registerVo, String yzm) {
        String mailNum = getMailNum();
        System.out.println("mailnum"+mailNum);
        if(!mailNum.equals(yzm)) {
            throw new IllegalArgumentException("验证码输入有误");
        }
        RegisterVo row = findRegisterVoByUserName(registerVo);
        if (row!=null){
            throw  new ServiceExecption("此用户名已经注册");
        }
        int register = registerDao.insert(registerVo);
         if (register==0){
             throw  new ServiceExecption("注册失败");
         }
    }

    @Override
    public RegisterVo findRegisterVoByUserName(RegisterVo registerVo) {
        QueryWrapper<RegisterVo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username", registerVo.getUsername());
        RegisterVo row = registerDao.selectOne(queryWrapper);
        System.out.println(row);
        return row;
    }
}
